package lab03.Test08;

import java.io.*;

/**
 * Created by Administrator on 2017/12/10.
 */
public class Test08 {
    public static void main(String[] args) throws IOException {
        String path1 = "D:\\dev\\shiyan\\Test08\\1.txt";
        String path2 = "D:\\dev\\shiyan\\Test08\\2.txt";
        String path3 = "D:\\dev\\shiyan\\Test08\\3.txt";

        InputStreamReader reader = new InputStreamReader(new FileInputStream(path1));
        BufferedReader buffer = new BufferedReader(reader);//读1.txt内容

        InputStreamReader reader1 = new InputStreamReader(new FileInputStream(path2));
        BufferedReader buffer1 = new BufferedReader(reader1);//读2.txt内容

        OutputStreamWriter last = new OutputStreamWriter(new FileOutputStream(path3));
        BufferedWriter writer = new BufferedWriter(last);//写3.txt内容

        String line1 = buffer.readLine();//
        String line2 = buffer1.readLine();
        while (true) {

            //4种情况 1.两个都有内容 2.1有2没有 3.2有1没有 4.都没有
            if (line1 != null && line2 != null) {//1
                System.out.println(line1);
                System.out.println(line2);
                writer.write(line1);//写1
                writer.newLine();
                writer.write(line2);//写2
                writer.newLine();
                writer.flush();
                line1 = buffer.readLine();
                line2 = buffer1.readLine();
            } else if (line1 == null && line2 != null) {//3
                System.out.println(line2);
                writer.write(line2);
                writer.newLine();
                writer.flush();
                line2 = buffer1.readLine();
            } else if (line1 != null && line2 == null) {//2
                System.out.println(line1);
                writer.write(line1);
                writer.newLine();
                writer.flush();
                line1 = buffer.readLine();
            } else {//4
                break;//终止死循环
            }
        }
    }
}

